//
//  TravelTableView.m
//  ShareNavigation
//
//  Created by 水木 傅 on 12-5-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TravelTableViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "TravelCell.h"
#import "UIImageCategory.h"
#import "SignTravel.h"

@implementation TravelTableViewController

@synthesize valueArray;

-(void)viewDidUnload
{
    valueArray = nil;
    [super viewDidUnload];
}

-(UIImage *) requestImage:(NSString *)imageUUID
{
    NSString *urlStr = [NSString stringWithFormat:@"http://localhost/~walker/ShareNavigationSVN/trunk/ShareNavigationPHP/upload/%@.jpg",imageUUID];
    NSURL *url  =[NSURL URLWithString: urlStr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *archivePath = [documentsDirectory 
                             stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"%@.jpg",imageUUID]];
    
    [request setDownloadDestinationPath:archivePath];
    return [UIImage imageWithContentsOfFile:archivePath];
}


//---  这个是返回tablview的列数的  ---
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//---  返回的是tableview的行数，一共有多少行  ---
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%d",[self.valueArray count]);
    return [self.valueArray count];
}

//返回每一个对应的单元cell的东西
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelCell *cell = (TravelCell *)[tableView dequeueReusableCellWithIdentifier:@"TravelCell"];
    SignTravel *sign = [self.valueArray objectAtIndex:indexPath.row];
    cell.UUID = sign.UUID;
    cell.addressLabel.text = sign.Location;
    cell.timeLabel.text = sign.Time;
    cell.signTextView.text = sign.TravelSignature;
    if(sign.hasImage == TRUE)
    {
        //UIImage *image =  [[self requestImage:sign.UUID] transformWidth:81 height:81];
        UIImage *image = [self requestImage:sign.UUID]; 
        cell.signImageView.image  = image;
    }
    else
    {
        cell.signImageView.image = [[UIImage imageNamed:@"null.jpg"] transformWidth:81 height:81];
    }
   // [cell.imageView setClipsToBounds:YES];

    return cell;
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSURL *url = [NSURL URLWithString:@"http://localhost/~walker/ShareNavigationSVN/trunk/ShareNavigationPHP/DeleteTravel.php"];
        ASIFormDataRequest *request=[[ASIFormDataRequest alloc] initWithURL:url];
        SignTravel *sign = [self.valueArray objectAtIndex:indexPath.row];
        [request setRequestMethod:@"POST"];
        [request setPostValue:sign.UUID forKey:@"UUID"];
        [request setDefaultResponseEncoding:NSUTF8StringEncoding];
        //---  发送请求信息  ----
        [request startSynchronous];
        NSLog(@"delete:  %@",[request responseString]);
        NSError *error = [request error];
        if(!error &&
           ([[request responseString] isEqualToString:@"TRUE"]))
        {
            [self.valueArray removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]
                     initWithTitle:@""
                     message:@"出错！"
                     delegate:self
                     cancelButtonTitle:@"返回"
                     otherButtonTitles:nil];
            [alert show];
        }       
    }
}


@end
